{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['eat', 'tea', 'ate'], ['tan', 'nat'], ['bat']]\n"
     ]
    }
   ],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def groupAnagrams(self, strs: List[str]) -> List[List[str]]:\n",
    "        # 定义哈希表\n",
    "        mp = collections.defaultdict(list)\n",
    "\n",
    "        # 遍历字符，将出现的字符作为索引\n",
    "        for st in strs:\n",
    "            key = \"\".join(sorted(st))\n",
    "            mp[key].append(st)\n",
    "\n",
    "        return list(mp.values())\n",
    "    \n",
    "if __name__ == '__main__':\n",
    "    strs = [\"eat\", \"tea\", \"tan\", \"ate\", \"nat\", \"bat\"]\n",
    "    print(Solution().groupAnagrams(strs))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pylearn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
